package com.app.controller;

import com.app.model.CustomerModel;
import com.app.model.TrainEnrollModel;
import com.app.model.UserModel;
import com.app.model.UserPolicyInfoModel;
import com.app.service.CustomerService;
import com.app.service.TrainEnrollService;
import com.app.service.UserPolicyInfoService;
import com.app.service.UserService;
import com.app.util.AESTool;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;
import java.util.Map;


@Controller
@ResponseBody
public class UserPolicyInfoController {


    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    UserPolicyInfoService userPolicyInfoService;

    @Autowired
    private CustomerService customerService;
    @Autowired
    private UserService userService;

    //增
    @PostMapping("/addUserPolicyInfo")
    public Result<Object> addUserPolicyInfo(
            @RequestParam(required = false) Integer seat,
            @RequestParam(required = false) Integer identity,
            @RequestParam(required = false) String schoolName,
            @RequestParam(required = false) String major,
            @RequestParam(required = false) Integer education,
            @RequestParam(required = false) Integer workingCondition,
            @RequestParam(required = false) Integer qualificationsState,
            @RequestParam(required = false) Integer titleState,
            @RequestParam(required = false) Integer industry,
            HttpServletRequest request
    ) throws ParseException {
        UserPolicyInfoModel model = new UserPolicyInfoModel();
        String token = request.getHeader("token");
        if(token==null)
        {
            return Result.error(405,"token不存在");
        }
        String mobile = TokenUtils.verify(token);
        UserModel user = userService.getUserByMobile(mobile);
        if(user==null)
        {
            return Result.error(405,"用户不存在");
        }
        CustomerModel cus = customerService.getCustomerById(user.getCId());
        model.setUserId(user.getId());
        model.setSeat(seat);
        model.setIdentity(identity);
        model.setMajor(major);
        model.setEducation(education);
        model.setWorkingCondition(workingCondition);
        model.setQualificationsState(qualificationsState);
        model.setTitleState(titleState);
        model.setIndustry(industry);
        try {
                model.setCrtTime(CommonHelp.getNowTimestamp());
            userPolicyInfoService.insert(model);

        }catch (Exception e)
        {
            System.out.println(e);
        }
        return Result.success(null);
    }
    //**
    // 根据token查询
    // *
    @PostMapping("/getUserPolicyInfo")
    @ResponseBody
    public Result<Object> getUserPolicyInfo(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405,"token不存在");
        }
        String tokenMobile = TokenUtils.verify(token);
        UserModel user = userService.getUserByMobile(tokenMobile);
        UserPolicyInfoModel model = userPolicyInfoService.getModelByUserId(user.getId());
        if (model != null) {
            return Result.success(1);
        } else {
            return Result.success(2);
        }
    }

    //**
    // 修改用户密码
    // *
    @PostMapping("/doUserUpdatePassWord")
    @ResponseBody
    public Result<Object> doUserUpdatePassWord(HttpServletRequest request,String passWord,String mobileCode) {
        String token = request.getHeader("token");
        if(token==null)
        {
            return Result.error(405,"token不存在");
        }
        String mobile = TokenUtils.verify(token);
        UserModel user = userService.getUserByMobile(mobile);
        if(user==null)
        {
            return Result.error(405,"用户不存在");
        }
        String sql = "SELECT * FROM tb_msg_identifiy WHERE mi_mobile='" + mobile + "' AND mi_idenCode = '" + mobileCode + "'";
        List<Map<String, Object>> identifiylist_maps = jdbcTemplate.queryForList(sql);
        if (identifiylist_maps == null || identifiylist_maps.size() == 0) {
            return Result.success(406);
        }
        String keyWord = AESTool.dealAESEncrypt(passWord,"szjy");
        user.setPassWord(keyWord);
        userService.update(user);
        return Result.success("修改成功");
    }



}
